package br.com.cast.scc.veiculo;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.feedback.ExactLevelFeedbackMessageFilter;
import org.apache.wicket.feedback.FeedbackMessage;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import br.com.cast.scc.enums.StatusRegistroAtivo;
import br.com.cast.scc.model.DominioTipoVeiculo;
import br.com.cast.scc.service.DominioTipoVeiculoService;
import br.com.cast.scc.service.VeiculoService;

public class ConsultarVeiculosIndexForm extends Form<FiltroPesquisaVeiculos> {
	private static final long serialVersionUID = 1L;
	
	@SpringBean(name = "dominioTipoVeiculoServiceImpl")
	private DominioTipoVeiculoService tipoVeiculoService;

	@SpringBean(name = "veiculoServiceImpl")
	private VeiculoService veiculoService;

	private VeiculoGridPanel gridPanel;

	private WebMarkupContainer panelCamposTerrestres;

	private WebMarkupContainer panelCamposFluviais;

	public ConsultarVeiculosIndexForm(String id) {
		super(id);

		add(new FeedbackPanel("msgErro", 
				new ExactLevelFeedbackMessageFilter(
					FeedbackMessage.ERROR)).setOutputMarkupId(true));

			add(new FeedbackPanel("msgSucesso",
				new ExactLevelFeedbackMessageFilter(
					FeedbackMessage.SUCCESS)));
		
		criaPaineisCampos();

		FiltroPesquisaVeiculos filtro = new FiltroPesquisaVeiculos(
				veiculoService);

		setDefaultModel(new CompoundPropertyModel<FiltroPesquisaVeiculos>(
				filtro));

		criaCampoTipoVeiculo();
		
		criaCampoNuEmbarcacao();
		
		criaCampoPlaca();
		
		criaCampoRntc();
		
		criaCampoStatus();
		
		criaGradeVeiculos(filtro);
		
		add(panelCamposTerrestres, panelCamposFluviais);
		
		criaBotaoPesquisar();
		
		criaBotaoNovo();
		
	}

	private void criaBotaoNovo() {
		add(new Link<Void>("novo"){
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(CadastrarVeiculoPage.class);
				
			}
			
		});
		
	}

	private void criaBotaoPesquisar() {
		add(new AjaxButton("pesquisar") {
			private static final long serialVersionUID = 1L;
		
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form){
				target.add(gridPanel);
			}
		});
		
	}

	private void criaGradeVeiculos(FiltroPesquisaVeiculos filtro) {
		gridPanel = new VeiculoGridPanel("panelVeiculos", filtro);
		add(gridPanel);
		
	}

	private void criaCampoStatus() {
		IChoiceRenderer<StatusRegistroAtivo> rendererStatus = new ChoiceRenderer<>("descricao");
		
		add(new RadioChoice<>("status", StatusRegistroAtivo.valores(),rendererStatus));
	}

	private void criaCampoRntc() {
		panelCamposTerrestres.add(new TextField<>("rntrc"));
		
	}

	private void criaCampoPlaca() {
		panelCamposTerrestres.add(new TextField<>("placa"));
		
	}

	private void criaCampoNuEmbarcacao() {
		panelCamposFluviais.add(new TextField<>("nuEmbarcacao"));
		
	}

	private void criaCampoTipoVeiculo() {
		IChoiceRenderer<DominioTipoVeiculo> renderer = new ChoiceRenderer<>(
				"descricao");

		final DropDownChoice<DominioTipoVeiculo> tipoVeiculo = new DropDownChoice<DominioTipoVeiculo>(
				"tipoVeiculo", tipoVeiculoService.consultaTodos(), renderer);
		tipoVeiculo.add(new AjaxFormComponentUpdatingBehavior("change") {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				DominioTipoVeiculo tipo = tipoVeiculo.getModelObject();
				
				boolean isTerrestre = tipo.getCodigo().equals(DominioTipoVeiculo.COD_TERRESTRE);
				
				panelCamposTerrestres.setVisible(isTerrestre);
				panelCamposFluviais.setVisible(!isTerrestre);
				
				target.add(panelCamposTerrestres, panelCamposFluviais);
			}
		});
		
		add(tipoVeiculo);
		
	}

	private void criaPaineisCampos() {
		panelCamposTerrestres = new WebMarkupContainer("panelCamposTerrestres");
		panelCamposTerrestres.setVisible(false);
		panelCamposTerrestres.setOutputMarkupPlaceholderTag(true);

		panelCamposFluviais = new WebMarkupContainer("panelCamposFluviais");
		panelCamposFluviais.setVisible(false);
		panelCamposFluviais.setOutputMarkupPlaceholderTag(true);

	}

}
